from paralib.tasks import *
from logging import info, debug, warning, error
import logging
import logging
import csv
import scipy.misc
from os.path import exists
from os import mkdir
from scipy.ndimage.filters import median_filter
import matplotlib.pyplot as plt
import plotly.plotly as py
import numpy as np
from scipy.ndimage.morphology import binary_opening, binary_closing
import pbcvt
import xml.etree.ElementTree as et
import os
import celery

HOME_DIR = os.environ['HOME']
DB_ROOT = HOME_DIR + '/data/ddb1_v02_01/'
TRAIN_SET_FILE = HOME_DIR + '/data/ddb1_v02_01/ddb1_v02_01_train_plain.txt'
TEST_SET_FILE = HOME_DIR + '/data/ddb1_v02_01/ddb1_v02_01_test_plain.txt'
TMP_OUT = 'out_dir/'


if __name__ == '__main__':
    FORMAT = '[%(levelname)-5s]%(asctime)-8s %(filename)s:%(lineno)d %(message)s'
    DATEFORMAT = '%H:%M:%S'
    logging.basicConfig(level=logging.INFO, format=FORMAT, datefmt=DATEFORMAT)

    info('Process start')

    if not exists(TMP_OUT):
        mkdir(TMP_OUT)

    spamreader = []
    csvfile = open(TEST_SET_FILE, 'r')
    spamreader = csv.reader(csvfile, delimiter=' ')
    index = 0
    res = []
    for row in spamreader:
        imgfilename = row[0]

        img = imread.s(imgfilename)
        cord = extractGroundTruth(row[1:])
        ca = img | candateSelection.s(index)
        # info('performance %f' % eval(ca, cord))
        evalres = ca | eval.s(cord)
        cawithPoint = ca | drawPoint.s(cord)
        index += 1
        res.append(cawithPoint)
    group = celery.group(res)()
    group.get()

